using UnityEngine;
using System.Collections.Generic;

public enum EquipmentType
{
    None,
    Weapon,
    Armor,
    Accessory,
    Amulet,
    Flask
}

[CreateAssetMenu(fileName = "New Item Data Equipment", menuName = "Data/Equipment", order = 1)]
public class ItemDataEquipment : ItemData
{
    public EquipmentType equipmentType;
    [Header("Unique effect")]
    public float itemCooldown;
    public ItemEffect[] itemEffects;
    [Header("Major Stats")]
    public int strength;
    public int agility;
    public int intelligence;
    public int vitality;
    [Header("Offensive Stats")]
    public int damage;
    public int criticalChance;
    public int criticalDamage;
    [Header("Defensive Stats")]
    public int health;
    public int armor;
    public int evasion;
    public int magicResistance;
    [Header("Magical Stats")]
    public int fireDamage;
    public int iceDamage;
    public int lightningDamage;
    [Header("Crafting Requirements")]
    public List<InventoryItem> craftingMaterials;

    public void Effect(Transform transform, Player player)
    {
        foreach (var item in itemEffects)
            item?.ExecuteEffect(transform, player);
    }

    public void AddModifiers()
    {
        // Example method to add a modifier to the equipment
        // This could be used to increase attack power, defense, etc.
        // Implement logic based on your game's requirements
        PlayerStats playerStats = FindAnyObjectByType<PlayerStats>();
        playerStats.strength.AddModifier(strength);
        playerStats.agility.AddModifier(agility);
        playerStats.intelligence.AddModifier(intelligence);
        playerStats.vitality.AddModifier(vitality);
        playerStats.damage.AddModifier(damage);
        playerStats.critChance.AddModifier(criticalChance);
        playerStats.critPower.AddModifier(criticalDamage);
        playerStats.maxHp.AddModifier(health);
        playerStats.armor.AddModifier(armor);
        playerStats.evasion.AddModifier(evasion);
        playerStats.magicResistance.AddModifier(magicResistance);
        playerStats.fireDamage.AddModifier(fireDamage);
        playerStats.iceDamage.AddModifier(iceDamage);
        playerStats.lightningDamage.AddModifier(lightningDamage);
    }

    public void RemoveModifiers()
    {
        // Example method to remove a modifier from the equipment
        // Implement logic based on your game's requirements
        var playerStats = FindAnyObjectByType<PlayerStats>();
        playerStats.strength.RemoveModifier(strength);
        playerStats.agility.RemoveModifier(agility);
        playerStats.intelligence.RemoveModifier(intelligence);
        playerStats.vitality.RemoveModifier(vitality);
        playerStats.damage.RemoveModifier(damage);
        playerStats.critChance.RemoveModifier(criticalChance);
        playerStats.critPower.RemoveModifier(criticalDamage);
        playerStats.maxHp.RemoveModifier(health);
        playerStats.armor.RemoveModifier(armor);
        playerStats.evasion.RemoveModifier(evasion);
        playerStats.magicResistance.RemoveModifier(magicResistance);
        playerStats.fireDamage.RemoveModifier(fireDamage);
        playerStats.iceDamage.RemoveModifier(iceDamage);
        playerStats.lightningDamage.RemoveModifier(lightningDamage);
    }

    public override string GetDescription()
    {
        _ = sb.Clear();
        AddItemDescription(strength, "Strength");
        AddItemDescription(agility, "Agility");
        AddItemDescription(intelligence, "Intelligence");
        AddItemDescription(vitality, "Vitality");
        AddItemDescription(damage, "Damage");
        AddItemDescription(criticalChance, "Critical Chance");
        AddItemDescription(criticalDamage, "Critical Damage");
        AddItemDescription(health, "Health");
        AddItemDescription(armor, "Armor");
        AddItemDescription(evasion, "Evasion");
        AddItemDescription(magicResistance, "Magic Resistance");
        AddItemDescription(fireDamage, "Fire Damage");
        AddItemDescription(iceDamage, "Ice Damage");
        AddItemDescription(lightningDamage, "Lightning Damage");
        _ = sb.AppendLine();
        for (int i = 0; i < itemEffects.Length; i++)
        {
            if (itemEffects[i].description.Length > 0)
            {
                _ = sb.AppendLine();
                _ = sb.Append($"Unique: {itemEffects[i].description}");
            }
        }
        return sb.ToString();
    }

    private void AddItemDescription(int itemValue, string itemName)
    {
        if (itemValue != 0)
            _ = sb.AppendLine(itemValue > 0 ? $" + {itemValue} {itemName}" : $" - {itemValue} {itemName}");
    }
}